package com.chinatsp.onlineapp.service;

import tsp.cn.http.interfac.INetResult;
import tsp.cn.http.tools.LogUtil;
import tsp.cn.http.tools.PreferenceUtils;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;

import com.chinatsp.onlineapp.base.Config;
import com.chinatsp.onlineapp.base.Constant;
import com.chinatsp.onlineapp.presenter.WeatherServiceDao;
import com.chinatsp.onlineapp.utils.TimeTool;

public class NetWeatherService extends Service implements INetResult {

	WeatherReceiver weatherReceiver;
	private Context mContext;

	public NetWeatherService() {
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	private WeatherServiceDao mWeatherServiceDao;
	private String defaultCity = Constant.PLACE_HOLDER;
	private String weatherCity1 = Constant.PLACE_HOLDER;
	private String weatherCity2 = Constant.PLACE_HOLDER;
	
	private int defaultCityRequestCode = 100;
	private int city1RequestCode = 101;
	private int city2RequestCode = 102;
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			LogUtil.e("接收数据"+msg.what);
		};
	};

	public void onCreate() {
		mContext = this;
		IntentFilter filter = new IntentFilter();
		filter.addAction(Constant.WEATHER_ACTION);
		weatherReceiver = new WeatherReceiver();
		this.registerReceiver(weatherReceiver, filter);
	};

	public void onStart(Intent intent, int startId) {
		Log.e("NetWeatherService", "WeatherService start");

		defaultCity = PreferenceUtils.getPreference(Constant.DEFAULT_CITY,Constant.PLACE_HOLDER);
		weatherCity1 = PreferenceUtils.getPreference(Constant.WEATHER_CITY1,Constant.PLACE_HOLDER);
		weatherCity2 = PreferenceUtils.getPreference(Constant.WEATHER_CITY2,Constant.PLACE_HOLDER);
		
		String last_date = PreferenceUtils.getPreference(Constant.WEATHER_UPDATE_TIME, "1");		
		String current_date = TimeTool.getSystemTime();		
		long diff_date = Long.valueOf(current_date)-Long.valueOf(last_date);
		// 测试大于60秒更新
		if (diff_date < 60) {
			LogUtil.e("时间未到，数据不缓存");
			return;
		}
		LogUtil.i(defaultCity + "&&" + weatherCity1 + "&&" +weatherCity2);

		new Thread(){
			public void run() {
				while(true){
					SystemClock.sleep(Config.NET_CD_TIME);
					int what = 1;
					handler.sendEmptyMessage(what);
					
				}
				
				
			};
		}.start();
		if (!defaultCity .equals(Constant.PLACE_HOLDER) ) {
			mWeatherServiceDao = new WeatherServiceDao(mContext,
					NetWeatherService.this);
			mWeatherServiceDao.getWeatherService(defaultCity);
			LogUtil.e("默认城市");

		}
		if (!weatherCity1 .equals(Constant.PLACE_HOLDER)) {
			mWeatherServiceDao = new WeatherServiceDao(mContext,
					NetWeatherService.this);
			mWeatherServiceDao.getWeatherService(weatherCity1);
			LogUtil.e("默认城市１");
			
		}
		if (!weatherCity2 .equals(Constant.PLACE_HOLDER)) {
			mWeatherServiceDao = new WeatherServiceDao(mContext,
					NetWeatherService.this);
			mWeatherServiceDao.getWeatherService(weatherCity2);
			LogUtil.e("默认城市２");
		}
		
		
	};

	@Override
	public boolean stopService(Intent name) {

		return super.stopService(name);
	}


	private class WeatherReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			if (!intent.getAction().equals(Constant.WEATHER_ACTION)) {
				return;
			}			
		}		
	}
	

	@Override
	public void onRequestSuccess(int requestCode) {
		String info = "数据缓存 "+"返回码"+requestCode  + mWeatherServiceDao.getmWeatherService().getResp_data().getCity_name();
		LogUtil.e(info);
		PreferenceUtils.savePreference(Constant.WEATHER_UPDATE_TIME, TimeTool.getSystemTime());
		if (requestCode == defaultCityRequestCode ) {
//			String result = mWeatherServiceDao.getWeatherResult();
//			PreferenceUtils.savePreference(Constant.WEATHER_CITY1,result);
		}else if(requestCode == city1RequestCode){
//			String result = mWeatherServiceDao.getWeatherResult();
//			PreferenceUtils.savePreference(Constant.WEATHER_CITY1, result);
		}else if (requestCode == city2RequestCode) {
//			String result = mWeatherServiceDao.getWeatherResult();
//			PreferenceUtils.savePreference(Constant.WEATHER_CITY1, result);
		}

	}

	@Override
	public void onRequestFailed(String errorNo, String errorMessage) {
		LogUtil.e("网络访问失败错误");
	}

	@Override
	public void onFinished() {

	}

	@Override
	public void onNoConnect() {
		LogUtil.e("没有网络链接");
	}

}
